import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.*;
import java.io.*;
import javax.swing.border.TitledBorder;
import java.awt.Color;
import java.net.*;

public class User extends JFrame implements ActionListener{
	
	StringTokenizer token;

	private static String id;
	private static String pw;
	private static String level;
	private static HashMap<Integer, Integer> scoreMap = new HashMap<Integer, Integer>();
	private static ArrayList<HashMap<Integer, Integer>> Map = new ArrayList<HashMap<Integer, Integer>>();
	private int score = 0;
	
	private JPanel contentPane;
	private JButton btnStartStudy;
	private JButton btnMidtermTest;
	private JButton btnLevelChange;
	private JButton btnRank;
	private JButton btnWrongVoca;
	private JButton btnLog;
	private JButton btnExit;
	private ImageIcon back;
	private ImageIcon b;
	private JFrame lcFrame;
	private JPanel contentPane2;
	private JButton btnChange;
	private JRadioButton rdbHigh;
	private JRadioButton rdbMid;
	private JRadioButton rdbtnLow;
	private Image Img;
	private Image ig;
	private Image a;
	private Image adImg;
	private Image addImg;
	private Image add1Img;
	private Image add2Img;
	public User() {
		back=new ImageIcon("Welcom.png");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 412, 480);
		contentPane = new JPanel(){
			public void paintComponent(Graphics g) {
	            
                g.drawImage(back.getImage(), 0, 0, null);
                
                setOpaque(false);
                super.paintComponent(g);
            }
		};
		
		contentPane.setBorder(new TitledBorder(null, "Moi", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(255, 0, 0)));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		
		btnStartStudy = new JButton(){ public void paintComponent(Graphics g) {
            Img = new ImageIcon("startstudy.png").getImage();
            g.drawImage(Img, 0, 0, this);
     }};
		btnStartStudy.addActionListener(this);
		btnStartStudy.setBounds(142, 111, 107, 31);
		contentPane.add(btnStartStudy);
		
		btnMidtermTest = new JButton(){ public void paintComponent(Graphics g1) {
            ig = new ImageIcon("midtermtest.png").getImage();
            g1.drawImage(ig, 0, 0, this);
     }};
		btnMidtermTest.addActionListener(this);
		btnMidtermTest.setBounds(142, 153, 107, 31);
		contentPane.add(btnMidtermTest);
		
		btnLevelChange = new JButton(){ public void paintComponent(Graphics g2) {
            a = new ImageIcon("Level.png").getImage();
            g2.drawImage(a, 0, 0, this);
     }};
		btnLevelChange.addActionListener(this);
		btnLevelChange.setBounds(142, 194, 107, 31);
		contentPane.add(btnLevelChange);
		
		btnRank = new JButton(){ public void paintComponent(Graphics g3) {
            adImg = new ImageIcon("rank.png").getImage();
            g3.drawImage(adImg, 0, 0, this);
     }};
		btnRank.addActionListener(this);
		btnRank.setBounds(142, 235, 107, 31);
		contentPane.add(btnRank);
		
		btnWrongVoca = new JButton(){ public void paintComponent(Graphics g4) {
            addImg = new ImageIcon("wrongvoca.png").getImage();
            g4.drawImage(addImg, 0, 0, this);
     }};
		btnWrongVoca.addActionListener(this);
		btnWrongVoca.setBounds(142, 276, 107, 31);
		contentPane.add(btnWrongVoca);
		
		btnLog = new JButton(){ public void paintComponent(Graphics g5) {
            add1Img = new ImageIcon("logout.png").getImage();
            g5.drawImage(add1Img, 0, 0, this);
     }};
		btnLog.setBounds(142, 317, 107, 31);
		contentPane.add(btnLog);
		
		btnExit = new JButton(){ public void paintComponent(Graphics g6) {
            add2Img = new ImageIcon("ex.png").getImage();
            g6.drawImage(add2Img, 0, 0, this);
     }};
		btnExit.addActionListener(this);
		btnExit.setBounds(142, 358, 107, 31);
		contentPane.add(btnExit);
	}

	public void levelChange() {
		
				lcFrame = new JFrame("Level change");
				b=new ImageIcon("Levelback.png");
				setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				lcFrame.setBounds(100, 100, 412, 300);
				contentPane2 = new JPanel(){
					public void paintComponent(Graphics g10) {
			            
		                g10.drawImage(b.getImage(), 0, 0, null);
		                
		                setOpaque(false);
		                super.paintComponent(g10);
		            }
				};
				contentPane2.setBorder(new TitledBorder(null, "Moi", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(255, 0, 0)));
				setContentPane(contentPane2);
				contentPane2.setLayout(null);
				
				rdbHigh = new JRadioButton("High");
				rdbHigh.setBounds(49, 166, 77, 27);
				contentPane2.add(rdbHigh);
				
				rdbMid = new JRadioButton("Mid");
				rdbMid.setBounds(180, 166, 77, 27);
				contentPane2.add(rdbMid);
				
				rdbtnLow = new JRadioButton("Low");
				rdbtnLow.setBounds(308, 166, 77, 27);
				contentPane2.add(rdbtnLow);
				
				ButtonGroup group = new ButtonGroup();
				group.add(rdbHigh);
				group.add(rdbMid);
				group.add(rdbtnLow);
				
				JLabel lblLevelChange = new JLabel("Level change");
				lblLevelChange.setBounds(162, 97, 103, 38);
				contentPane2.add(lblLevelChange);
				
				btnChange = new JButton("Change");
				btnChange.addActionListener(this);
				btnChange.setBounds(327, 226, 105, 27);
				contentPane2.add(btnChange);

			    
			    lcFrame.getContentPane().add(contentPane2);
				
				lcFrame.setVisible(true);

		
			}
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(btnExit)) {
			try {
				FileOutputStream fos = new FileOutputStream(this.getId()+".dat");
				ObjectOutputStream oos = new ObjectOutputStream(fos);
				oos.writeObject(this);
				oos.close();
				fos.close();
			} catch (Exception err) {
				err.printStackTrace();
			}
			System.exit(0);
		} else if (e.getSource().equals(btnLog)) {
			try {
				FileOutputStream fos = new FileOutputStream(this.getId()+".dat");
				ObjectOutputStream oos = new ObjectOutputStream(fos);
				oos.writeObject(this);
				oos.close();
				fos.close();
			} catch (Exception err) {
				err.printStackTrace();
			}
			Education edu = new Education();
			edu.setVisible(true);
			dispose();
		} else if (e.getSource().equals(btnStartStudy)) {
			if (User.level.equals("high"))
				new StudyHigh().setVisible(true);
			else if(User.level.equals("mid"))
				new StudyMid().setVisible(true);
			else if(User.level.equals("low"))
				new StudyLow().setVisible(true);
		} else if (e.getSource().equals(btnMidtermTest)) {
			// Midterm Test
		} else if (e.getSource().equals(btnRank)) {
			if(User.Map.size() == 0)
				Map.add(0, User.scoreMap);
			else {
				Map.set(0, User.scoreMap);
			}
			new Rank();
		} else if (e.getSource().equals(btnWrongVoca)) {
			// Vocabulary
		} else if (e.getSource().equals(btnLevelChange)) {
			levelChange();
		}
		if (e.getSource().equals(btnChange)) {

			int count = 0;
			String tempString = null;
			ArrayList<String[]> arr = new ArrayList<String[]>();
			try {
				BufferedReader br = new BufferedReader(new FileReader(
						"user.txt"));

				String tempid = null;

				if (rdbHigh.isSelected() == true)
					tempString = "high";
				else if (rdbMid.isSelected() == true)
					tempString = "mid";
				else if (rdbtnLow.isSelected() == true)
					tempString = "low";

				if (tempString.equals(User.getLevel())) {
					JOptionPane
							.showMessageDialog(
									lcFrame,
									"selected level must be different with current level.",
									"LevelChange", JOptionPane.ERROR_MESSAGE);
					lcFrame.dispose();
					dispose();
					new User().setVisible(true);
				} else {

					int temp = new Enroll().calcUserCount();
					while (true) {

						if (count == temp)
							break;

						token = new StringTokenizer(br.readLine(), "/");
						tempid = token.nextToken();

						String[] st = new String[3];

						if (tempid.equals(this.id)) {
							st[0] = tempid;
							st[1] = token.nextToken();
							st[2] = tempString;
							token.nextToken();
						} else {
							st[0] = tempid;
							st[1] = token.nextToken();
							st[2] = token.nextToken();
						}

						arr.add(st);
						count++;

					}

					FileWriter bw = new FileWriter("user.txt", false);

					for (int i = 0; i < arr.size(); i++) {
						bw.append(arr.get(i)[0]);
						bw.append("/");
						bw.append(arr.get(i)[1]);
						bw.append("/");
						bw.append(arr.get(i)[2]);
						bw.append("\n");
					}
					User.level = tempString;
					JOptionPane.showMessageDialog(lcFrame,
							"Level change succeed!", "Level change",
							JOptionPane.INFORMATION_MESSAGE);
					lcFrame.dispose();
					dispose();
					new User().setVisible(true);

					bw.close();
					br.close();
				}
			} catch (IOException error) {
				JOptionPane.showMessageDialog(lcFrame, "Level change failed!",
						"Level change", JOptionPane.ERROR_MESSAGE);
				lcFrame.dispose();
				error.printStackTrace();

			}

		}
		
	}

	public static String getId() {
		return id;
	}

	public static void setId(String id) {
		User.id = id;
	}

	public static String getPw() {
		return pw;
	}

	public static void setPw(String pw) {
		User.pw = pw;
	}

	public static String getLevel() {
		return level;
	}

	public static void setLevel(String level) {
		User.level = level;
	}

	public int getScore() {
		return score;
	}

	public void setScore(int score) {
		this.score = score;
	}

	public static HashMap<Integer, Integer> getScoreMap() {
		return scoreMap;
	}

	public static void setScoreMap(HashMap<Integer, Integer> scoreMap) {
		User.scoreMap = scoreMap;
	}

	public static int getScoreMapIndex(int i) {
		return scoreMap.get(i);
	}

	public static void setScoreMapIndex(int i, int value) {
		User.scoreMap.put(i, value);
	}

	public static int getScoreMapSize() {
		return User.scoreMap.size();
	}

	public static ArrayList<HashMap<Integer, Integer>> getMap() {
		return Map;
	}

	public static void setMap(ArrayList<HashMap<Integer, Integer>> map) {
		Map = map;
	}

	public HashMap<Integer, Integer> getMap(int i) {
		return Map.get(i);
	}

	public HashMap<Integer, Integer> removeMap(int i) {
		return Map.remove(i);
	}

	public void addMap(int i, HashMap<Integer, Integer> map) {
		Map.add(i, map);
	}

	public void addMap(HashMap<Integer, Integer> map) {
		Map.add(map);
	}

	public int getMapSize() {
		return Map.size();
	}

	

}

